package com.jhhc.IoUtils;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class SsDao {
    /**
     * 一行一行导入 比较慢
     * @throws IOException
     */
    public void insert1() throws IOException {
        List<String> list = null;

        String line = null;
        FileReader fileReader = null;
        BufferedReader bufferedReader = null;
        DBHelper dbHelper = null;
        String sql = null;
        try {
            list = DataGather.fileTest("C:\\Users\\Administrator\\Desktop\\txt");
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (int i = 0; i < list.size(); i++) {
            String name = list.get(i).substring(39, 52);
            switch (name) {
                case "ST_PPTN_R.txt":
                    fileReader = new FileReader("C:\\Users\\Administrator\\Desktop\\txt\\txt\\ST_PPTN_R.txt");
                    bufferedReader = new BufferedReader(fileReader);
                    sql = "insert into ST_PPTN_R(STCD, TM, DRP, INTV, PDR,DYP,WTH) values(?,?,?,?,?,?,?)";
                    dbHelper = new DBHelper();
                    while ((line = bufferedReader.readLine()) != null) {
                        String[] arr = line.split(",");
                        dbHelper.update(sql, arr);
                    }
                    bufferedReader.close();
                    break;
                case "ST_RIVER_R.tx":
                    fileReader = new FileReader("C:\\Users\\Administrator\\Desktop\\txt\\txt\\ST_RIVER_R.txt");
                    bufferedReader = new BufferedReader(fileReader);

                    sql = "insert into ST_RIVER_R(STCD, TM, Z, Q ,XSA,XSAVV,XSMXV,FLWCHRCD, WPTN, MSQMT, MSAMT, MSVMT) values(?,?,?,?,?,?,?,?,?,?,?,?)";
                    dbHelper = new DBHelper();

                    while ((line = bufferedReader.readLine()) != null) {
                        String[] arr = line.split(",");
                        dbHelper.update(sql, arr);
                    }
                    bufferedReader.close();
                    break;
                case "ST_RSVR_R.txt":
                    fileReader = new FileReader("C:\\Users\\Administrator\\Desktop\\txt\\txt\\ST_RSVR_R.txt");
                    bufferedReader = new BufferedReader(fileReader);

                    sql = "insert into ST_RSVR_R(STCD,TM,RZ, INQ, W, BLRZ, OTQ,RWCHRCD,RWPTN,INQDR,MSQMT) values(?,?,?,?,?,?,?,?,?,?,?)";
                    dbHelper = new DBHelper();

                    while ((line = bufferedReader.readLine()) != null) {
                        String[] arr = line.split(",");
                        dbHelper.update(sql, arr);
                    }
                    bufferedReader.close();
                    break;
            }

        }

    }

    /**
     * 一次性导入全部
     * @throws IOException
     */
    public void insert2() throws IOException{
        List<String> list =null;

        String line = null;
        StringBuffer sb =null;
        FileReader fileReader =null;
        BufferedReader bufferedReader=null;
        DBHelper dbHelper =null;
        List<Object> param =null;
        try {
            list  = DataGather.fileTest("C:\\Users\\Administrator\\Desktop\\txt");
        } catch (Exception e) {
            e.printStackTrace();
        }
        for(int i=0;i<list.size();i++){
            String name = list.get(i).substring(39, 52);
            switch (name) {
                case "ST_PPTN_R.txt":
                     fileReader = new FileReader("C:\\Users\\Administrator\\Desktop\\txt\\txt\\ST_PPTN_R.txt");
                     bufferedReader = new BufferedReader(fileReader);

                   // String sql = "insert into st_pptn_r(STCD, TM, DRP, INTV, PDR,DYP) values(?,?,?,?,?,?)";
                     dbHelper = new DBHelper();
                    sb  = new StringBuffer("insert into ST_PPTN_R(STCD, TM, DRP, INTV, PDR,DYP,WTH) values");
                    param = new ArrayList<>();
                    while ( (line = bufferedReader.readLine()) != null){
                        sb.append("(?,?,?,?,?,?,?))");
                        String[] arr = line.split(",");
                        //            for (String s:arr){
                        //                param.add(s);
                        //            }
                        param.addAll(Arrays.asList(arr));

                    }
                    dbHelper.update(sb.toString().substring(0,sb.length() - 1) ,param.toArray());
                    System.out.println("结束");
                    bufferedReader.close();
                    break;
                case "ST_RIVER_R.tx":
                    fileReader= new FileReader("C:\\Users\\Administrator\\Desktop\\txt\\txt\\ST_RIVER_R.txt");
                     bufferedReader = new BufferedReader(fileReader);

                   // String sql = "insert into st_pptn_r(STCD, TM, DRP, INTV, PDR,DYP) values(?,?,?,?,?,?)";
                     dbHelper = new DBHelper();
                    sb  = new StringBuffer("insert into ST_RIVER_R(STCD, TM, Z, Q ,XSA,XSAVV,XSMXV,FLWCHRCD, WPTN, MSQMT, MSAMT, MSVMT) values");
                     param = new ArrayList<>();
                    while ( (line = bufferedReader.readLine()) != null){
                        sb.append("(?,?,?,?,?,?,?,?,?,?,?,?))");
                        String[] arr = line.split(",");
                        //            for (String s:arr){
                        //                param.add(s);
                        //            }
                        param.addAll(Arrays.asList(arr));

                    }
                    dbHelper.update(sb.toString().substring(0,sb.length() - 1) ,param.toArray());
                    System.out.println("结束");
                    bufferedReader.close();
                    break;
                case "ST_RSVR_R.txt":
                     fileReader = new FileReader("C:\\Users\\Administrator\\Desktop\\txt\\txt\\ST_RSVR_R.txt");
                     bufferedReader = new BufferedReader(fileReader);

                    //String sql = "insert into st_rsvr_r(STCD, TM, DRP, INTV, PDR,DYP) values(?,?,?,?,?,?)";
                     dbHelper = new DBHelper();
                    sb  = new StringBuffer("insert into ST_RSVR_R(STCD,TM,RZ, INQ, W, BLRZ, OTQ,RWCHRCD,RWPTN,INQDR,MSQMT) values");
                     param = new ArrayList<>();
                    while ( (line = bufferedReader.readLine()) != null){
                        sb.append("(?,?,?,?,?,?,?,?,?,?,?))");
                        String[] arr = line.split(",");
                        //            for (String s:arr){
                        //                param.add(s);
                        //            }
                        param.addAll(Arrays.asList(arr));

                    }
                    dbHelper.update(sb.toString().substring(0,sb.length() - 1) ,param.toArray());
                    System.out.println("结束");
                    bufferedReader.close();
                    break;
            }
        }
//        FileReader fileReader = new FileReader(filename);
//        BufferedReader bufferedReader = new BufferedReader(fileReader);
//
//        String line = null;
//        String sql = "insert into st_pptn_r(STCD, TM, DRP, INTV, PDR,DYP) values(?,?,?,?,?,?)";
//
//        DBHelper dbHelper = new DBHelper();
//        StringBuffer sb = new StringBuffer("insert into st_pptn_r(STCD, TM, DRP, INTV, PDR,DYP) values");
//        List<Object> param = new ArrayList<>();
//        while ( (line = bufferedReader.readLine()) != null){
//            sb.append("(?,?,?,?,?,?))");
//            String[] arr = line.split(",");
////            for (String s:arr){
////                param.add(s);
////            }
//            param.addAll(Arrays.asList(arr));
//
//        }
//        dbHelper.update(sb.toString().substring(0,sb.length() - 1) ,param.toArray());
//        System.out.println("结束");
//        bufferedReader.close();
    }

    public static void main(String[] args) {
        String filename = "C:\\Users\\Administrator\\Desktop\\txt\\txt\\ST_PPTN_R.txt";
        try {
            new SsDao().insert1();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public List<Map<String,Object>> queryAll(){
        return new DBHelper().query("select * from ST_PPTN_R");
    }

}


